package config

import (
	"fmt"
	"gopkg.in/ini.v1"
	"os"
)

var (
	RunMode  string
	HttpPort string

	Type     string
	Host     string
	Port     string
	Db       string
	User     string
	Password string
)

func init() {
	var (
		err  error
		file *ini.File
		conf *ini.File
		path string
	)

	file, err = ini.Load("config/conf.ini")
	if err != nil {
		fmt.Println("读取失败！")
		return
	}
	LoadServer(file)
	switch RunMode {
	case "debug":
		path = "config/dev.ini"
	case "release":
		path = "config/release.ini"
	default:
		fmt.Println("读取失败！")
		os.Exit(0)
	}
	conf, err = ini.Load(path)
	LoadData(conf)
}

func LoadServer(file *ini.File) {
	RunMode = file.Section("server").Key("RunMode").MustString("debug")
	HttpPort = file.Section("server").Key("HttpPort").MustString(":8090")
}

func LoadData(file *ini.File) {
	Type = file.Section("database").Key("Type").MustString("mysql")
	Host = file.Section("database").Key("Host").MustString("localhost")
	Port = file.Section("database").Key("Port").MustString("3306")
	Db = file.Section("database").Key("Db").MustString(":ginblog")
	User = file.Section("database").Key("User").MustString(":root")
	Password = file.Section("database").Key("Password").MustString(":123456")
}
